package pl.edu.agh.winmon.widget;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;

public class TimeCombo extends Composite {

	private Combo combo;

	private Button calendarButton;

	public TimeCombo(Composite arg0, int arg1) {
		super(arg0, arg1);
		setLayout(new GridLayout(2, false));
		combo = new Combo(this, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.minimumWidth = 220;
		combo.setLayoutData(gd);
		calendarButton = new Button(this, SWT.PUSH);
		calendarButton.setText("...");
		calendarButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				openCalendar();
			}
		});
	}

	public void addSelectionListener(SelectionListener listener) {
		combo.addSelectionListener(listener);
	}

	public Date getDate() {
		return new Date((Long) combo.getData(combo.getItem(combo
				.getSelectionIndex())));
	}

	public void setParticularDate(Date date) {
		
		String dateStr = date.toString();
		if (!combo.getItem(combo.getSelectionIndex()).equals(dateStr)) {
			for (int i = 0; i < combo.getItemCount(); i++) {
				if (combo.getItem(i).equals(dateStr)) {
					combo.select(i);
					break;
				}
			}
			combo.notifyListeners(SWT.Selection, new Event());
		}
	}

	public void setDate(Date date) {
		Long time = date.getTime();

		for (int i = 0; i < combo.getItemCount() - 1; i++) {
			Long time1 = (Long) combo.getData(combo.getItem(i));
			Long time2 = (Long) combo.getData(combo.getItem(i + 1));

			if (time.equals(time1)) {
				setParticularDate(date);
				break;
			}

			if (time.equals(time2)) {
				setParticularDate(date);
				break;
			}

			if (time > time1 && time < time2) {
				if (time - time1 < time2 - time) {
					setParticularDate(new Date(time1));
				} else {
					setParticularDate(new Date(time2));
				}
				break;
			}
		}

	}

	public void addDate(Date date) {
		combo.add(date.toString());
		combo.setData(date.toString(), date.getTime());
	}

	public void removeAll() {
		combo.removeAll();
	}

	public void select(int i) {
		combo.select(i);
	}

	public int getItemCount() {
		return combo.getItemCount();
	}

	private void openCalendar() {
		Display display = Display.getCurrent();
		final Shell dialog = new Shell(display);

		dialog.setLayout(new GridLayout(1, true));

		final DateTime time = new DateTime(dialog, SWT.TIME);
		time.setTime(getDate().getHours(), getDate().getMinutes(), getDate()
				.getSeconds());

		final DateTime calendar = new DateTime(dialog, SWT.CALENDAR
				| SWT.BORDER);
		calendar.setDate(getDate().getYear(), getDate().getMonth(), getDate()
				.getDay());

		final Button ok = new Button(dialog, SWT.PUSH);
		ok.setText("OK");
		ok.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

		SelectionAdapter selectionAdapter = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Date date = new Date(calendar.getYear() - 1900,
						calendar.getMonth(), calendar.getDay(),
						time.getHours(), time.getMinutes(), time.getSeconds());
				Long t1 = ((Long) combo.getData(combo.getItem(0)));
				Long t2 = (Long) combo.getData(combo.getItem(combo
						.getItemCount() - 1));
				Long t = date.getTime();
				if (t1 <= t && t2 >= t) {
					ok.setEnabled(true);
				} else {
					ok.setEnabled(false);
				}
			}
		};

		calendar.addSelectionListener(selectionAdapter);
		time.addSelectionListener(selectionAdapter);

		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				Date date = new Date(calendar.getYear() - 1900, calendar
						.getMonth(), calendar.getDay(), time.getHours(), time
						.getMinutes(), time.getSeconds());
				setDate(date);
				dialog.close();
			}
		});

		dialog.setDefaultButton(ok);
		dialog.pack();
		dialog.open();

		while (!dialog.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
